//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/times.h>
#include <sys/time.h>
#include <sys/wait.h>

#include <fcntl.h>
#include <unistd.h>
#include <time.h>

#include <errno.h>

#include <iostream>

#include "ArffAttributeSelection.h"
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
using namespace std;
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
int main (int argc, char** argv) {
  
  String* namefileBuffer = NULL;
  String* inputFile      = NULL;
  
  if (argc < 4) {
    
    cout << "Error in the input parameters! Must by like: " << endl;
    cout << "./CreateCommandWekaRemoveAttr <Filename with attributes selection buffer>";
    cout <<                              " <Path of input file (commands queue file)>";
    cout <<                              " ...";
    cout << endl;
    
    return 0;
    
  }
  else {
    
    namefileBuffer = new String(argv[1]);
    inputFile      = new String(argv[2]);
    
    cout << "----------------------------------------------------------------------------------------------------------------" << endl;
    cout << " Filename with attributes selection buffer : " << argv[1] << endl;
    cout << "   Path of input file (commands queue file : " << argv[2] << endl;    
    cout << "----------------------------------------------------------------------------------------------------------------" << endl;
    
  }
  
  ArffAttributeSelection* arffAttributeSelection = new ArffAttributeSelection ();
  
  cout << "Preparing the files..." << endl;
  arffAttributeSelection->FilenameIn(namefileBuffer);
  
  cout << "Loading the data..." << endl;
  if (!arffAttributeSelection->LoadSelectionResult(NULL))
    cout << "Error loading data!" << endl;
  
  cout << "Building the line command to launch in the server..." << endl;
  
  char* list_param [argc];
  
  list_param[0] = (char*) malloc(strlen("echo") * sizeof(char));
  strcpy(list_param[0], "echo");
  
  for (int i = 0, j = 1; i < argc - 3; i++) {
    
    list_param[j] = (char*) malloc((strlen(argv[i + 3])) * sizeof(char));
    strcpy(list_param[j++], argv[i + 3]);
    
    if (!strcmp(argv[i + 3], "-R")) {
      String* patternsPos = arffAttributeSelection->OutPositionPatterns(1);
      list_param[j] = (char*) malloc((strlen(patternsPos->Out()) + 3) * sizeof(char));
      sprintf(list_param[j++], "1,%s\0", patternsPos->Out());
    }
    
  }

  list_param[argc - 1]= '\0';
  
  if (arffAttributeSelection) delete arffAttributeSelection;
  
  if (namefileBuffer)         delete namefileBuffer;
  if (inputFile)              delete inputFile;

  int pidFork;
  int status;
  
  pidFork = fork();

  switch (pidFork) {

    case -1 : cout << "Error redirecting the result to " << inputFile << " ..." << endl; 
              break;

    case 0  : {
              int file = open(inputFile->Out(), O_APPEND);
    
              if (file > 0) {
                
                dup2(file, STDOUT_FILENO);
                execvp(list_param[0], list_param);
                cout << "Error redirecting the result to " << inputFile << " ..." << endl;
                
              }
              else cout << "Error redirecting the result to " << inputFile << " ..." << endl;
    
              exit(EXIT_SUCCESS);
    }
    default : pidFork = wait(&status);

  }
    
    
  cout << "Finished..." << endl << "Good Bye" << endl;
  
  return EXIT_SUCCESS;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//

